Sys.Application.add_load(ClientEventsInit);

var m_Events = [];
var m_EventCode = [];
var m_CurrentControlID = 'DNNMenu';
var m_PageNS;
var m_EventsPanel;
var m_DefinitionPanel;
var m_OutputControl;
var m_NavControl = [];

m_Events['DNNMenu'] = {events: [{name: 'click', compat: false, arg: 'DNNNodeEventArgs'},
                                {name: 'callBackStatus', compat: true, arg: 'result, context'},
                                {name: 'callBackSuccess', compat: false, arg: 'DNNCallbackEventArgs'},
                                {name: 'callBackFail', compat: false, arg: 'DNNCallbackEventArgs'}]};
m_Events['DNNTree'] = {events: [{name: 'click', compat: false, arg: 'DNNNodeEventArgs'},
                                {name: 'callBackStatus', compat: true, arg: 'result, context'},
                                {name: 'callBackSuccess', compat: false, arg: 'DNNCallbackEventArgs'},
                                {name: 'callBackFail', compat: false, arg: 'DNNCallbackEventArgs'}]};
m_Events['DNNLabelEdit'] = {events: [{name: 'beforeSave', compat: true, arg: 'Sys.CancelEventArgs'},
                                {name: 'beforeEdit', compat: false, arg: 'evt, lecontrol' },
                                {name: 'callBackStatus', compat: true, arg: 'result, context'},
                                {name: 'callBackSuccess', compat: false, arg: 'DNNCallbackEventArgs'},
                                {name: 'callBackFail', compat: false, arg: 'DNNCallbackEventArgs'}]};
m_Events['DNNTabStrip'] = {events: [{name: 'selectedIndexChanged', compat: true, arg: 'evt, tscontrol'},
                                {name: 'tabClick', compat: false, arg: 'DNNTabStripEventArgs'},
                                {name: 'callBackStatus', compat: true, arg: 'result, context, req'},
                                {name: 'callBackSuccess', compat: false, arg: 'DNNCallbackEventArgs'},
                                {name: 'callBackFail', compat: false, arg: 'DNNCallbackEventArgs'}]};
m_Events['DNNTextSuggest'] = {events: [{name: 'click', compat: false, arg: 'DNNNodeEventArgs'},
                                {name: 'callBackStatus', compat: true, arg: 'result, context, req'},
                                {name: 'callBackSuccess', compat: false, arg: 'DNNCallbackEventArgs'},
                                {name: 'callBackFail', compat: false, arg: 'DNNCallbackEventArgs'}]};
m_Events['DNNToolBar'] = {events: [{name: 'click', compat: false, arg: 'DNNToolBarEventArgs'}]};


m_EventCode['DNNNodeEventArgs'] = '&nbsp;var ctl = src;<br/>&nbsp;var node = arg.get_node();<br/>&nbsp;arg.set_cancel(true);<br/>}';
m_EventCode['DNNCallbackEventArgs'] = '&nbsp;var res = arg.get_result();<br/>&nbsp;var ctx = arg.get_context();<br/>&nbsp;var request = arg.get_request();<br/>}';
m_EventCode['result, context'] = '&nbsp;var res = result;<br/>&nbsp;var ctx = context;<br/>}';
m_EventCode['result, context, req'] = '&nbsp;var res = result;<br/>&nbsp;var ctx = context;<br/>&nbsp;var request = req;<br/>}';
m_EventCode['evt, lecontrol'] = "&nbsp;return confirm('Are you sure you wish to save this text?\n' + lecontrol.editWrapper.getText());<br/>}";
m_EventCode['evt, tscontrol'] = "&nbsp;var tab = tscontrol.selectedTab;<br/>}";
m_EventCode['DNNTabStripEventArgs'] = "&nbsp;var text = tab.text;<br/>}";
m_EventCode['DNNToolBarEventArgs'] = "&nbsp;var btn = arg.get_button();<br/>}";

function ClientEventsInit()
{
    m_PageNS = dnn.getVar('PageNS');
    m_EventsPanel = $get('divEvents');
    m_DefinitionPanel = $get('preDefinition');
    m_OutputControl = $get('txtOutput');
    dnn.controls.find('mainTabStrip').setSelectedIndex(0);
}

function tsControls_SelectedIndexChanged(evt, tabStrip)
{
    clearOutput();
    if (m_CurrentControlID)
    {
        var events = m_Events[m_CurrentControlID].events;
        var evt;
        for (var i=0; i<events.length; i++)
        {
            evt = events[i];
            var func = eval(m_CurrentControlID + evt.name + '_handler');
            if (m_NavControl[m_CurrentControlID] != null)
                m_NavControl[m_CurrentControlID].remove_handler(evt.name, func);
        }    
    }
    
    m_CurrentControlID = tabStrip.selectedTab.text;
    refreshCheckBoxes();
    return true;

}

function refreshCheckBoxes()
{
    m_EventsPanel.innerHTML = '';
    var events = m_Events[m_CurrentControlID].events;
    for (var i=0; i<events.length; i++)
    {
        m_EventsPanel.appendChild(createCheckBox(events[i]));
    }
    
}

function createCheckBox(evt)
{
    var div = document.createElement('div');
    div.id = 'div' + evt.name;
    var chk = document.createElement('input');
    chk.type = 'checkbox';
    chk.id = 'chk' + evt.name;
    div.appendChild(chk);
    $addHandler(chk, 'click', eventChecked);
    var span = document.createElement('span');
    span.innerHTML = evt.name;
    div.appendChild(span);
    return div;
}

function eventChecked(evtArg)
{
    clearOutput();
    var chk = evtArg.target;
    var evt = getEventByName(chk.id.substring(3));
    refreshEventDef(evt);
    var func = eval(m_CurrentControlID + evt.name + '_handler');
    
    if (m_NavControl[m_CurrentControlID] == null)
        m_NavControl[m_CurrentControlID] = dnn.controls.find(m_CurrentControlID);

    if (m_NavControl[m_CurrentControlID] != null)
    {
        if (chk.checked)
            m_NavControl[m_CurrentControlID].add_handler(evt.name, func);
        else
            m_NavControl[m_CurrentControlID].remove_handler(evt.name, func);
    }
    else
    {
        chk.disabled = true;    
        chk.checked = false;
        alert('control not loaded');
    }
}

function refreshEventDef(evt)
{
    var def = 'function ' + m_CurrentControlID + evt.name + '_handler(';
    if (evt.compat)
        def += evt.arg + ')<br/>{<br/>'
    else
        def += 'src, ' + evt.arg + ')<br/>{<br/>';
        
    m_DefinitionPanel.innerHTML = def + m_EventCode[evt.arg];
}

function getEventByName(name)
{
    for (var i=0; i<m_Events[m_CurrentControlID].events.length; i++)
    {
        if (m_Events[m_CurrentControlID].events[i].name == name)
            return m_Events[m_CurrentControlID].events[i];
    }
}

function writeOutput(s)
{
    m_OutputControl.value += s + '\r\n';
}
function clearOutput()
{
    m_OutputControl.value = '';
}

//Menu functions
function DNNMenuclick_handler(src, arg)
{ 
    writeNodeClick('DNNMenu_click', src, arg);
    arg.set_cancel(true);
}

function DNNMenucallBackStatus_handler(result, context, req)
{ 
    writeCallbackCompat('DNNMenucallBackStatus', result, context, req);
}

function DNNMenucallBackSuccess_handler(result, arg)
{ 
    writeCallback('DNNMenucallBackSuccess', result, arg);
}

function DNNMenucallBackFail_handler(result, arg)
{ 
    writeCallback('DNNMenucallBackFail', result, arg);
}

//Tree functions
function DNNTreeclick_handler(src, arg)
{ 
    writeNodeClick('DNNTree_click', src, arg);
    arg.set_cancel(true);
}

function DNNTreecallBackStatus_handler(result, context, req)
{ 
    writeCallbackCompat('DNNMenucallBackStatus', result, context, req);
}

function DNNTreecallBackSuccess_handler(result, arg)
{ 
    writeCallback('DNNMenucallBackSuccess', result, arg);
}

function DNNTreecallBackFail_handler(result, arg)
{ 
    writeCallback('DNNMenucallBackFail', result, arg);
}

//LabelEdit functions
function DNNLabelEditcallBackStatus_handler(result, context, req)
{ 
    writeCallbackCompat('DNNLabelEditcallBackStatus', result, context, req, 'ns');
}

function DNNLabelEditcallBackSuccess_handler(result, arg)
{ 
    writeCallback('DNNLabelEditcallBackSuccess', result, arg, 'ns');
}

function DNNLabelEditcallBackFail_handler(result, arg)
{ 
    writeCallback('DNNLabelEditcallBackFail', result, arg, 'ns');
}

function DNNLabelEditselectedIndexChanged_handler(evt, tscontrol)
{
    writeOutput('DNNLabelEditselectedIndexChanged_handler: ' + evt + ', ' + tscontrol);
    writeOutput('tscontrol.selectedTab.tabId: ' + tscontrol.selectedTab.tabId);
}

function DNNLabelEditbeforeSave_handler(evt, lecontrol)
{
    writeOutput('DNNTabStripbeforeSave_handler: ' + evt + ', ' + lecontrol);
    writeOutput('lecontrol.editWrapper.getText(): ' + lecontrol.editWrapper.getText());
    return confirm('Are you sure you wish to save this text?\n' + lecontrol.editWrapper.getText());
}

function DNNLabelEditbeforeEdit_handler(src, args)
{
    writeOutput('DNNLabelEditbeforeEdit_handler: ' + src + ', ' + args);
    writeOutput('src.editWrapper.getText(): ' + src.editWrapper.getText());
    //src.editWrapper.setText('before Edit setText works!');
    args.set_cancel(false);
}

//TabStrip functions
function DNNTabStripcallBackStatus_handler(result, context, req)
{ 
    writeCallbackCompat('DNNTabStripcallBackStatus', result, context, req, 'ns');
}

function DNNTabStripcallBackSuccess_handler(result, arg)
{ 
    writeCallback('DNNTabStripcallBackSuccess', result, arg, 'ns');
}

function DNNTabStripcallBackFail_handler(result, arg)
{ 
    writeCallback('DNNTabStripcallBackFail', result, arg, 'ns');
}

function DNNTabStripselectedIndexChanged_handler(evt, tscontrol)
{
    writeOutput('DNNLabelEditselectedIndexChanged_handler: ' + evt + ', ' + tscontrol);
    writeOutput('tscontrol.selectedTab.tabId: ' + tscontrol.selectedTab.tabId);
}


function DNNTabStriptabClick_handler(evt, arg)
{
    writeOutput('DNNTabStriptabClick_handler: ' + evt + ', ' + arg);
    writeOutput('arg.get_tab().tabId: ' + arg.get_tab().tabId);
}

//TextSuggest
function DNNTextSuggestclick_handler(src, arg)
{ 
    writeNodeClick('DNNTextSuggest_click', src, arg);
}

function DNNTextSuggestcallBackStatus_handler(result, context, req)
{ 
    writeCallbackCompat('DNNTextSuggestcallBackStatus', result, context, req);
}

function DNNTextSuggestcallBackSuccess_handler(result, arg)
{ 
    writeCallback('DNNTextSuggestcallBackSuccess', result, arg);
}

function DNNTextSuggestcallBackFail_handler(result, arg)
{ 
    writeCallback('DNNTextSuggestcallBackFail', result, arg);
}

//ToolBar
function DNNToolBarclick_handler(src, arg)
{ 
    writeOutput('DNNToolBarclick_handler: ' + src + ', ' + arg);
    writeOutput('arg.get_button().key: ' + arg.get_button().key);
    arg.set_cancel(!confirm('Are you sure you wish to perform this action?'));
}


//Helpers

function writeNodeClick(name, src, arg)
{
    writeOutput(name + '_handler: ' + src + ', ' + arg);
    writeOutput('src.ns: ' + src.ns);
    writeOutput('arg.get_node().id: ' + arg.get_node().id);
}

function writeCallbackCompat(name, result, context, req, prop)
{
    writeOutput(name + '_handler: ' + result + ', ' + context + ', ' + req);
    if (prop == null)
        prop = 'id';
    writeOutput('context.' + prop + ': ' + context[prop]);   //context=node
    
    if (req)
        writeOutput('req.completed: ' + req.completed); 
}

function writeCallback(name, src, arg, prop)
{
    writeOutput(name + '_handler: ' + src + ', ' + arg);
    writeOutput('src.ns: ' + src.ns);
    writeOutput('arg.get_result(): ' + arg.get_result());
    var context = arg.get_context();
    if (prop == null)
        prop = 'id';
    writeOutput('arg.get_context().' + prop + ': ' + context[prop]);
    writeOutput('arg.get_request().completed: ' + arg.get_request().completed);
}





